package com.lc.proxy;

import com.lc.executor.Executor;
import com.lc.executor.impl.DefaultExecutor;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


public class DaoProxy implements InvocationHandler {

    private Executor executor = new DefaultExecutor();

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, args);
        }

        Class<?> returnType = method.getReturnType();
        String methodName = method.getName();
        if (methodName.startsWith("select")) {
            return executor.executeQuery(returnType, method, args);
        } else if (methodName.startsWith("insert")) {

        } else if (methodName.startsWith("update")) {

        } else if (methodName.startsWith("delete")) {

        }
        return null;
    }


    public static void main(String[] args) throws Exception {
       /* User user = new User();
        Class aClass = user.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            field.set(user, 2);
            break;
        }

        System.out.println("id: " + user.getId());*/

/*        List<Object> list = new ArrayList<>();
        System.out.println(list.getClass().getInterfaces()[0].getName());*/

        Class target = Class.forName("com.lc.demo.User");
        Field[] declaredFields = target.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getName() + " ==type== " + declaredField.getType().getName());
        }
    }
}
